package br.com.fiap.si.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.fiap.si.bean.*;
import br.com.fiap.factory.ConnectionFactory;


public class UsuarioDAO {

	Connection conn = null;

	public UsuarioDAO() {

		try {
			conn = ConnectionFactory.getConnection();
		} catch (SQLException ex) {
		}
	}

	public void insert(Usuario usuario) {

		String sql = "INSERT INTO usuario(login, senha) VALUES(?,?)";

		try {
			PreparedStatement stmt = conn.prepareStatement(sql);

			stmt.setString(1, usuario.getLogin());
			stmt.setString(2, usuario.getSenha());

			stmt.executeUpdate();

		} catch (SQLException ex) {

		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
			}
		}

	}

	public int login2(String login, String senha) {
		
		int idUser = -1;
		
		String sql = "SELECT * FROM usuario WHERE login = ? and senha = ?";

		try {
			PreparedStatement stmt = conn.prepareStatement(sql);

			stmt.setString(1, login);
			stmt.setString(2, senha);

			ResultSet rs = stmt.executeQuery();
			

			if (rs.next()){
				idUser = rs.getInt("IdUser");
				return idUser;
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
			}
		}

		return idUser;
	}

}
